﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CrossLayerCommunication;

namespace UnitTest
{
    [TestClass]
    public class TestMessageHelper
    {
        [TestMethod]
        public void TestAddParamsNoPreviousParams()
        {
            //arrange
            CrossLayerCommunication.Message.AllMessages.Clear();
            IMessage message = MessageHelper.CreateOrGetMessage(-1);

            //act
            var addedParamsMessage = message.AddParams(1, "Testing");

            //assert
            Assert.AreEqual(0, message.Params.Length);
            Assert.AreEqual(2, addedParamsMessage.Params.Length);
            Assert.AreEqual(1, addedParamsMessage.Params[0]); 
            Assert.AreEqual("Testing", addedParamsMessage.Params[1]);
        }

        [TestMethod]
        public void TestAddParamsPreviousParams()
        {
            //arrange
            Message.AllMessages.Clear();
            IMessage message = MessageHelper.CreateOrGetMessage(-1, arguments: new object[2] { 2, 1});

            //act
            var addedParamsMessage = message.AddParams(1, "Testing");

            //assert
            Assert.AreEqual(2, message.Params.Length);
            Assert.AreEqual(3, addedParamsMessage.Params.Length);
            Assert.AreEqual(2, addedParamsMessage.Params[0]);
            Assert.AreEqual(1, addedParamsMessage.Params[1]);
            Assert.AreEqual("Testing", addedParamsMessage.Params[2]);
        }

        [TestMethod]
        public void TestClearParams()
        {
            //arrange
            Message.AllMessages.Clear();
            IMessage message = MessageHelper.CreateOrGetMessage(-1, arguments: new object[2] { 2, 1 });

            //act
            var changedParamsMessage = message.ClearParams();

            //assert
            Assert.AreEqual(2, message.Params.Length);
            Assert.AreEqual(0, changedParamsMessage.Params.Length);
        }

        [TestMethod]
        public void TestChangeKey()
        {
            //arrange
            Message.AllMessages.Clear();
            IMessage message = MessageHelper.CreateOrGetMessage(-1, key: "Testing");

            //act
            var changedKeyMessage = message.ChangeKey("NewKey");

            //assert
            Assert.AreEqual("Testing", message.Key);
            Assert.AreEqual("NewKey", changedKeyMessage.Key);
        }
    }
}
